package com.hyg.lib_music.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.DataModel.Music.Music;
import com.hyg.lib_music.R;
import com.hyg.lib_music.modle.CategoryMusicListData;
import com.hyg.lib_music.ui.activity.MusicMainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Administrator
 * @date: 2021/4/8
 */
public class MusicPlayListAdapter extends RecyclerView.Adapter<MusicPlayListAdapter.ViewHolder> {

    ArrayList<Music> list;
    Context context;

    public MusicPlayListAdapter(Context context, ArrayList<Music>list) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_play_list, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Music music = list.get(position);
        String user = music.musicName + ((TextUtils.isEmpty(music.musician) || music.musician.equals("无")) ? "" : music.musician);
        holder.nameTv.setText(user);
//        holder.CollectionIv.setId(dataDTO.getId());
        holder.CollectionIv.initCheckbox();
        holder.CollectionIv.setCHecked(music.id,music.isFav);

        if (music.isEditable) {
            holder.isSelectCB.setVisibility(View.VISIBLE);
        } else {
            holder.isSelectCB.setVisibility(View.GONE);
        }
        holder.isSelectCB.setChecked(music.isChecked);
        if (music.isPlaying) {
            holder.playingHint.setVisibility(View.VISIBLE);
            holder.nameTv.setTextColor(context.getResources().getColor(R.color.mainMusicColor));
        } else {
            holder.playingHint.setVisibility(View.GONE);
            holder.nameTv.setTextColor(context.getResources().getColor(R.color.musicTextBlackDeep));
        }
        holder.isSelectCB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                music.isChecked=isChecked;
            }
        });

         holder.rl_root.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 MusicMainActivity.serviceBinder.addPlayList(list.get(position));
             }
         });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView nameTv;
        ImageView playingHint;
        CheckBox isSelectCB;
        com.hyg.lib_music.ui.view.MusicCheckBox CollectionIv;
        RelativeLayout rl_root;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTv = itemView.findViewById(R.id.tv_music_name);
            playingHint = itemView.findViewById(R.id.iv_playing_show);
            CollectionIv = itemView.findViewById(R.id.iv_collection);
            isSelectCB = itemView.findViewById(R.id.cb_select);
            rl_root = itemView.findViewById(R.id.rl_root);
        }
    }

}
